const mongoose = require("mongoose");
var Merchant = mongoose.model("merchant");
const request = require('request');
var webServiceProperties = require('../WebServiceProperties');
var merchantUrl = webServiceProperties.inativeMerchantUrl;
var imgUrl = webServiceProperties.imgUrl;

exports.loadFromSessionMid = function (req, res) {
    var _id = req.session.mid;
    Merchant.findOne({_id:_id}, function (err, merchant) {
        merchant.icon = imgUrl + merchant.icon;
        res.json(merchant);
    });
};

exports.loadActiveMerchant = function (req, res) {
    request(merchantUrl, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log("mercahnts from A" + body); // Show the HTML for the Google homepage.
            inactiveMerchants = JSON.parse(body);
            var mids = [];
            inactiveMerchants.forEach(function (item) {
                console.log("Merchants item" + item);
                mids.push(item._id);

            });
            console.log(mids);
            Merchant.find({_id: {$nin: mids}}, function (err, activeMerchants) {
                    if (!err) {
                        activeMerchants.forEach(function (item) {
                            item.icon = imgUrl + item.icon;
                            // console.log('get recommend dish success:' + dish.dicon)
                        });
                        res.json(activeMerchants);
                    }
                    else {
                        console.log("{'get merchant':'Fail'}");
                    }
                }
            );
        }
    })
};
